package com.orifine.orm.type;

import com.orifine.orm.Information;
import com.orifine.orm.Type;
import com.orifine.orm.information.CharRandomInfo;
import com.orifine.orm.model.ColumnModel;

public class VarChar extends Type {
	private String value;
	
	public VarChar(){
		
	}
	
	public VarChar(String value){
		this.value = value;
	}
	
	@Override
	public String getScriptType(ColumnModel columnMapper) {
		String lengthString = "";
		String typeString = "VARCHAR";
		if(columnMapper.getLength().getValue() == 0){
			lengthString = "(150)";
		} else if(columnMapper.getLength().getValue() < 0) {
			throw new RuntimeException(columnMapper.getField()+"的Length不能为负值");
		} else {
			lengthString = "("+columnMapper.getLength().getValue()+")";
		}
		return typeString + lengthString;
	}

	public String valueString(){
		return value;
	}
	
	@Override
	public String getScriptValue(ColumnModel column) {
		if(value == null) return "NULL";
		return filterQuote(value);
	}
	
	@Override
	public Information getDefaultInfo(ColumnModel column) {
		return new CharRandomInfo(column.getLength().getValue());
	}
	
	public static final int LENGTH_MAX = 191;
}
